(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "friendly-petite-vue"], factory);
    }
})(function (require, exports) {
    "use strict";
    exports.__esModule = true;
    var friendly_petite_vue_1 = require("friendly-petite-vue");
    exports["default"] = friendly_petite_vue_1.defineComponent(function (props) {
        return {
            template: "\n        <div class=\"calc_output\" ref=\"root\">\n            <input class=\"calc_output-result\" :value=\"output\" readonly>\n            <input class=\"calc_output-discount\" v-model=\"$store.discount\" readonly>\n            <span class=\"calc_output-percent\">%</span>\n        </div>\n        <div class=\"calc_btn\" v-for=\"btn in btns\" @click=\"handleClick(btn)\" :class=\"{sign: typeof btn !== 'number'}\">{{btn}}</div>\n        ",
            mounted: function () {
                this.$el.classList.add('calc');
            },
            data: function () {
                return {
                    btns: props && props.btns || [7, 8, 9, 'x', 4, 5, 6, '-', 1, 2, 3, '+', 0, '.', '/', '='],
                    sign: '+',
                    input: [],
                    sum: 0,
                    output: 0,
                    calced: false
                };
            },
            methods: {
                handleClick: function (btn) {
                    if (typeof btn === 'number') {
                        if (this.calced) {
                            this.sum = 0;
                        }
                        this.input.push(btn);
                        this.output = this.input.join('');
                    }
                    else if (btn === '.') {
                        if (this.calced) {
                            this.sum = 0;
                        }
                        if (this.input.indexOf('.') === -1) {
                            this.input.push('.');
                            this.output = this.input.join('');
                        }
                    }
                    else if (['+', '-', 'x', '/'].indexOf(btn) !== -1) {
                        if (this.input.length === 0)
                            return;
                        if (['+', '-'].indexOf(this.sign) !== -1) {
                            this.sum += Number(this.sign + (this.input.splice(0).join('') || '0'));
                        }
                        else if ('x' === this.sign) {
                            this.sum *= Number((this.input.splice(0).join('') || '1'));
                        }
                        else if ('/' === this.sign) {
                            this.sum /= Number((this.input.splice(0).join('') || '1'));
                        }
                        this.sign = btn;
                        this.output += btn;
                    }
                    else if (btn === '=') {
                        if (this.input.length === 0)
                            return;
                        if (['+', '-'].indexOf(this.sign) !== -1) {
                            this.sum += Number(this.sign + (this.input.splice(0).join('') || '0'));
                        }
                        else if ('x' === this.sign) {
                            this.sum *= Number((this.input.splice(0).join('') || '1'));
                        }
                        else if ('/' === this.sign) {
                            this.sum /= Number((this.input.splice(0).join('') || '1'));
                        }
                        this.sum = this.sum * (this.$store.discount / 100);
                        this.sign = '+';
                        this.output = this.sum;
                        this.calced = true;
                    }
                    if (btn !== '=' && this.calced) {
                        this.calced = false;
                    }
                }
            }
        };
    });
});
